package com.newgrand.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.newgrand.mapper.Fg3UnitaccountMapper;
import com.newgrand.mapper.UIPCommonMapper;
import com.newgrand.model.Fg3Unitaccount;
import com.newgrand.service.impl.UipLog;
import com.newgrand.utils.HRJdbcTemplate;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

/**
 * @Author: zhanglixin
 * @Data: 2022/11/16 12:45
 * @Description: TODO
 */
@Slf4j
@Api(tags = "测试用")
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private UIPCommonMapper uipCommonMapper;


    @Autowired
    private UipLog uipLog;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法
    @Value("${i8.databaseName}")
    private String databaseName;
    @Autowired
    private HRJdbcTemplate jdbcTemplate2;

    @Autowired
    private Fg3UnitaccountMapper fg3UnitaccountMapper;

    @RequestMapping(value = "/getdatatest", method = RequestMethod.GET)
    public @ResponseBody
    Object GetDataTest() throws Exception {
        try{
            JSONObject jo = new JSONObject();

            JSONObject push = new JSONObject();
            push.put("phid","123213");

            rabbitTemplate.convertAndSend("contract-exchange", "contract.info",push.toJSONString());
            return jo;
        }catch (Exception ex){
            return ex.getMessage();
        }
    }

    @RequestMapping(value = "/getHRtest", method = RequestMethod.GET)
    public @ResponseBody
    Object GetHRTest() throws Exception {
        try{
            List<Map<String, Object>> dataMap =uipCommonMapper.dynamicSql("select * from qkhr_empview");
            return dataMap;
        }catch (Exception ex){
            return ex.getMessage();
        }
    }

    @RequestMapping(value = "/getHRtest0", method = RequestMethod.GET)
    public @ResponseBody
    Object GetHRTest0() throws Exception {
        try{
            TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
            TimeZone.setDefault(timeZone);
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
            dataSource.setUrl("jdbc:oracle:thin:@192.168.2.201:1521/orcl");
            dataSource.setUsername("nafmiihr2");
            dataSource.setPassword("hrnafmiipwd");

            JdbcTemplate jdbcTemplate1 = new JdbcTemplate(dataSource);

            List<Map<String, Object>> dataMap =jdbcTemplate1.queryForList("select * from TUE01_QYWX");
            return dataMap;
        }catch (Exception ex){
            return ex.getMessage();
        }
    }
    @RequestMapping(value = "/getHRtest3", method = RequestMethod.GET)
    public @ResponseBody
    Object GetHRTest3() throws Exception {
        try{

            List<Map<String, Object>> dataMap =jdbcTemplate2.GetJdbcTemplate().queryForList("select * from TUE01_QYWX");
            return dataMap;
        }catch (Exception ex){
            return ex.getMessage();
        }
    }

    @RequestMapping(value = "/getHRtest2", method = RequestMethod.GET)
    public @ResponseBody
    Object GetHRTest2() throws Exception {
        try{
            List<Map<String, Object>> dataMap =uipCommonMapper.dynamicSql("select * from TUE01_QYWX");
            return dataMap;
        }catch (Exception ex){
            return ex.getMessage();
        }
    }

    @RequestMapping(value = "/getHRtest4", method = RequestMethod.GET)
    public @ResponseBody
    Object GetHRTest4() throws Exception {
        try{
            TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
            TimeZone.setDefault(timeZone);
            Class.forName("oracle.jdbc.OracleDriver");
            //3.获取数据库连接对象
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.2.201:1521/orcl", "nafmiihr2", "hrnafmiipwd");
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from TUE01_QYWX");
            while (rs.next()){
                //6.2 获取数据  getXxx()
                String name = rs.getString("name");

                uipLog.info("data", "da:",name);
            }
            String jod = JSONObject.toJSONString(rs);
            //7. 释放资源
            rs.close();
            stmt.close();
            conn.close();

            JSONObject dataMap = new JSONObject();

            //List<Map<String, Object>> dataMap =uipCommonMapper.dynamicSql("select * from TUE01_QYWX");
            return jod;
        }catch (Exception ex){
            return ex.getMessage();
        }
    }

    /**
     * 查询i8的组织架构数据
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getOrgDept", method = RequestMethod.GET)
    public @ResponseBody
    Object GetOrgDept() throws Exception {
        try{
            Date nowTime = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String now = sdf.format(nowTime);
            String querySql = "select * from fg_orglist ";
            List<Map<String, Object>> dataMap = uipCommonMapper.dynamicSql(querySql);
            return dataMap;
        }catch (Exception ex){
        }
        return null;
    }
}
